package com.lixingye.meowstar.controller;

import com.lixingye.meowstar.common.AppResult;
import com.lixingye.meowstar.common.ResultCode;
import com.lixingye.meowstar.exception.ApplicationException;
import com.lixingye.meowstar.model.Board;
import com.lixingye.meowstar.service.IBoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 猫大仙
 * @since 2025-01-14
 */
@Slf4j
@Tag(name = "板块接口")
@RestController
@RequestMapping("/board")
public class BoardController {
    @Autowired
    private IBoardService boardService;

    @Value("${meowstar-forum.index.board-num:9}")
    private Integer indexBoardNum;
    @Operation(summary = "获取板块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("首页板块个数为:{}",indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if(boards == null) boards = new ArrayList<>();
        return AppResult.success(boards);
    }
    @Operation(summary = "获取板块详情")
    @GetMapping("/getById")
    public AppResult<Board> getById(@Parameter(name = "id",description = "板块ID") @RequestParam("id") @NonNull Long id){
        Board board = boardService.getById(id);
        if(board == null || board.getDeleteState()==1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
